package com.six.backend.controller;

import com.six.backend.annotation.OperLog;
import com.six.backend.service.InformService;
import com.six.backend.shiro.Utils.Result;
import com.six.backend.utils.TokenUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: skyler
 * @Date: 2021/12/19 20:51
 */
@RestController
@Slf4j
@Transactional(rollbackFor=Exception.class)
@RequestMapping(value = "/form",produces = "application/json;charset = utf-8")
public class InformController {
    @Autowired
    private InformService informService;

    @ApiOperation("获取已读消息")
    @OperLog(operModule = "消息模块",operType = "get",operDesc = "获取已读消息")
    @GetMapping("getReadingInform")
    public Result selectReadingInform(HttpServletRequest request) {
        String account = TokenUtil.getTokenName(request);
        return new Result<>().setCode(200).setMessage("获取已读消息成功").setData(informService.selectReadingInform(account));
    }

    @ApiOperation("获取未读消息")
    @OperLog(operModule = "消息模块",operType = "get",operDesc = "获取未读消息")
    @GetMapping("getUnreadInform")
    public Result selectUnreadInform(HttpServletRequest request) {
        String account = TokenUtil.getTokenName(request);
        return new Result<>().setCode(200).setMessage("获取未读消息成功").setData(informService.selectUnReadInform(account));
    }

    @ApiOperation("删除消息")
    @OperLog(operModule = "消息模块",operType = "delete",operDesc = "删除消息")
    @DeleteMapping("deleteInform")
    public Result deleteInform(@RequestParam("id") Integer id) {
        if (informService.deleteInform(id)) {
            return new Result<>().setCode(200).setMessage("删除消息成功");
        }
        return new Result<>().setCode(502).setMessage("删除消息失败");
    }

    @ApiOperation("更新消息状态")
    @OperLog(operModule = "消息模块",operType = "put",operDesc = "更新消息")
    @PutMapping("updateInform")
    public Result updateInform(@RequestParam("id") Integer id) {
        if (informService.changeInformStatus(id)) {
            return new Result<>().setCode(200).setMessage("更新消息成功");
        }
        return new Result<>().setCode(502).setMessage("更新消息失败");
    }
}
